package com.dpxm.zdbdg.charte.sand.service;

import com.dpxm.zdbdg.charte.sand.dao.SanDAllDao;
import com.dpxm.zdbdg.charte.sand.dao.SanDDao;
import com.dpxm.zdbdg.charte.sand.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vehiclebase.enums.QueryEnum;
import vehiclebase.utils.ToolsUtils;

import java.awt.*;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HistoryEchartsService {
    @Autowired
    private SanDAllDao sanDAllDao;
    @Autowired
    private SanDDao sanDDao;
    public SandEchartsRes historyEcharts() {
        SandEchartsRes sandEchartsRes = new SandEchartsRes();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, List<String>>  mapRes = new HashMap<>();
        //查询出近20期的号码作为 X轴
        List<String> sanDkai = sanDDao.QueryMessage(QueryEnum.QUERY_QIHAO_SAND.val());
        //查询出近1100期的号码当中出现过近20期的重复号码
        List<SanDVO> sanDkaiALL = sanDDao.QueryMessageObj(QueryEnum.QUERY_QIHAO_SANDALL.val(),sanDkai);

        List<String> str=new ArrayList<>();//次数
        List<SandEchartsVo> sanDListRes=new ArrayList<>();//间隔天数


        sanDkaiALL.forEach(a->{
            str.add(a.getStrs());
            List<String> listnCreatedate = Arrays.asList(a.getCreatedateString().split(","));
            if(listnCreatedate.size()>1){
                SandEchartsVo sandEchartsVo = new SandEchartsVo();//Echarts间隔日期天数
                sandEchartsVo.setNubm(a.getNubm());
                List<String> daynubm=new ArrayList<>();//间隔天数
                List<String> day=new ArrayList<>();//天数
                for (int i=0;i<listnCreatedate.size();i++){
                    if(i>0){
                        // 日期格式化
                        Date startDate = null;
                        Date endDate= null;
                        try {
                            startDate = simpleFormat.parse(listnCreatedate.get(i-1));
                             endDate = simpleFormat.parse(listnCreatedate.get(i));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long startTime = startDate.getTime();
                        long endTime = endDate.getTime();
                        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
                        daynubm.add(days+"");
                        day.add(listnCreatedate.get(i-1)+"");
                        day.add(listnCreatedate.get(i)+"");
                    }
                }
                sandEchartsVo.setDateList(daynubm);
                sandEchartsVo.setDate(day);
                sanDListRes.add(sandEchartsVo);

            }else {
                SandEchartsVo sandEchartsVo = new SandEchartsVo();//Echarts间隔日期天数
                sandEchartsVo.setNubm(a.getNubm());
                List<String> daynubm=new ArrayList<>();//间隔天数
                List<String> day=new ArrayList<>();//天数
                daynubm.add("1");
                day.add(a.getCreatedateString());
                sandEchartsVo.setDateList(daynubm);
                sandEchartsVo.setDate(day);
                sanDListRes.add(sandEchartsVo);


            }

        });
        sandEchartsRes.setSanDkai(sanDkai);
        sandEchartsRes.setStr(str);
        sandEchartsRes.setSanDListRes(sanDListRes);

        return sandEchartsRes;

    }

    public SandEchartsRes historyEcharts25() {
        SandEchartsRes sandEchartsRes = new SandEchartsRes();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, List<String>>  mapRes = new HashMap<>();
        //查询出近20期的号码作为 X轴
        List<String> sanDkai = sanDDao.QueryMessage(QueryEnum.QUERY_QIHAO_SAND_LIU.val());
        //查询出近1100期的号码当中出现过近20期的重复号码
        List<SanDVO> sanDkaiALL = sanDDao.QueryMessageObj(QueryEnum.QUERY_QIHAO_SANDALL_REWU.val(),sanDkai);

        List<String> str=new ArrayList<>();//次数
        List<SandEchartsVo> sanDListRes=new ArrayList<>();//间隔天数


        sanDkaiALL.forEach(a->{
            str.add(a.getStrs());
            List<String> listnCreatedate = Arrays.asList(a.getCreatedateString().split(","));
            if(listnCreatedate.size()>1){
                SandEchartsVo sandEchartsVo = new SandEchartsVo();//Echarts间隔日期天数
                sandEchartsVo.setNubm(a.getNubm());
                List<String> daynubm=new ArrayList<>();//间隔天数
                List<String> day=new ArrayList<>();//天数
                for (int i=0;i<listnCreatedate.size();i++){
                    if(i>0){
                        // 日期格式化
                        Date startDate = null;
                        Date endDate= null;
                        try {
                            startDate = simpleFormat.parse(listnCreatedate.get(i-1));
                            endDate = simpleFormat.parse(listnCreatedate.get(i));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long startTime = startDate.getTime();
                        long endTime = endDate.getTime();
                        int days = (int) ((endTime - startTime) / (1000 * 60 * 60 * 24));
                        daynubm.add(days+"");
                        day.add(listnCreatedate.get(i-1)+"");
                        day.add(listnCreatedate.get(i)+"");
                    }
                }
                sandEchartsVo.setDateList(daynubm);
                sandEchartsVo.setDate(day);
                sanDListRes.add(sandEchartsVo);

            }else {
                SandEchartsVo sandEchartsVo = new SandEchartsVo();//Echarts间隔日期天数
                sandEchartsVo.setNubm(a.getNubm());
                List<String> daynubm=new ArrayList<>();//间隔天数
                List<String> day=new ArrayList<>();//天数
                daynubm.add("1");
                day.add(a.getCreatedateString());
                sandEchartsVo.setDateList(daynubm);
                sandEchartsVo.setDate(day);
                sanDListRes.add(sandEchartsVo);


            }

        });
        sandEchartsRes.setSanDkai(sanDkai);
        sandEchartsRes.setStr(str);
        sandEchartsRes.setSanDListRes(sanDListRes);

        return sandEchartsRes;

    }

    public SandEchartsZhuZhuangRes selectEchartsZhuZhuang(String qishu) {
        SandEchartsZhuZhuangRes sandEchartsZhuZhuangRes = new SandEchartsZhuZhuangRes();
        List<String> sanDCiShu = new ArrayList<>(); //左边Y周
        List<String> prob = new ArrayList<>();//右边Y轴
        //查询号码集合
        List<SanDVO> sanDList= sanDDao.QueryMessageSandVo(qishu);
        List<String> sanDAll = new ArrayList<>();
        sanDList.forEach(a->{
            sanDAll.add(a.getNubm().substring(0,1));
            sanDAll.add(a.getNubm().substring(1,2));
            sanDAll.add(a.getNubm().substring(2,3));
        });
        //List<String> 转化为List<Integer>
        List<Integer> codesInteger = sanDAll.stream().map(Integer::parseInt).collect(Collectors.toList());
        Map<Integer,Integer> map = new HashMap<>();
        for(Integer str:codesInteger){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(str) != null){
                i=map.get(str)+1;
            }
            map.put(str,i);
        }
        map.forEach((key,value)->{
            sanDCiShu.add(value.toString());
        });
        sandEchartsZhuZhuangRes.setSanDCiShu(sanDCiShu);
        return sandEchartsZhuZhuangRes;
    }



}
